<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 雲溪荏苒 <290648237@qq.com>
// +----------------------------------------------------------------------

namespace app\frontend\controller;

use controller\HomeBase;


/**
 * 新闻资讯控制器
 *
 * Class News
 * @package app\frontend\controller
 * @author 雲溪荏苒 <290648237@qq.com>
 * @date 2017/11/14
 */
class News extends HomeBase
{

    public function index()
    {
        $this->assign('generator', request()->domain().'/,cn,108,,2,M1156010');
        $this->assign('body_class', 'class-108');

        // 新闻列表
        list($newsList, $total) = model('cms_article')->getList(['article_state' => 3], 0, 5);
        $this->assign('newsList', $newsList);

        // 获取推荐文章列表
        $this->get_recommend_news();

//        dump($newsList);die;

        // 渲染分页
        $pageHtml = lay_page($total, '/news/');
        $this->assign('pageHtml', $pageHtml);

        return $this->fetch('index');
    }


    public function lists()
    {
        $articleClassName = '';
        $ac_id = I('ac_id', 0);
        $this->assign('cur_ac_id', $ac_id);

        $this->assign('generator', request()->domain().'/,cn,'.$ac_id.',,2,M1156010');
        $this->assign('body_class', 'class-'.$ac_id);

        // 获取文章列表
        $condition['article_state'] = 3;
        // 根据文章分类筛选
        if($ac_id){
            $condition['article_class_id'] = $ac_id;
            $articleClassName = D('cms_article_class')->getCategoryNameByID($ac_id);
        }
        // 根据搜索关键词筛选
        $keyword = I('keyword', '');
        if($keyword) {
            $condition['article_title'] = ['like', "%{$keyword}%"];
        }
        $this->assign('keyword', $keyword);
        $this->assign('articleClassName', $articleClassName);

        // 新闻列表
        list($newsList, $total) = model('cms_article')->getList($condition, 0, 5);
        $this->assign('newsList', $newsList);

        // 获取推荐文章列表
        $this->get_recommend_news($condition);

        // 渲染分页
        $pageHtml = lay_page($total, '/news_list/'.$ac_id);
        $this->assign('pageHtml', $pageHtml);

        // SEO
        $this->assign('seo_title', '新闻中心 - '.sysconf('cms_article_seo_title').' - '.sysconf('seo_title'));
        $this->assign('seo_keywords', sysconf('cms_article_seo_keywords').','.sysconf('seo_keywords'));
        $this->assign('seo_description', sysconf('cms_article_seo_desc').sysconf('seo_description'));

        return $this->fetch('lists');
    }


    public function show()
    {
        $articleId = I('get.article_id');

        // 获取新闻详情
        $articleInfo = D('cms_article')->getArticleDetail($articleId);
        $this->assign('articleInfo', $articleInfo);

        $this->assign('generator', request()->domain().'/,cn,'.$articleInfo['article_class_id'].',53,2,M1156010');
        $this->assign('body_class', 'class-'.$articleInfo['article_class_id']);
        $this->assign('cur_ac_id', $articleInfo['article_class_id']);

        // 获取推荐文章列表
        $this->get_recommend_news(['article_class_id' => $articleInfo['article_class_id']]);

        // 获取上一篇、下一篇文章
        list($previous, $next) = D('cms_article')->getFrontAfterArticles($articleId);
        $this->assign('previous', $previous);
        $this->assign('next', $next);

        // 文章访问量+1
        D('cms_article')->setViewInc($articleId);

        // SEO
        $this->assign('seo_title', $articleInfo['article_title'].' - '.$articleInfo['article_class_name'].' - 新闻动态');
        $this->assign('seo_keywords', sysconf('cms_article_seo_keywords').','.sysconf('seo_keywords'));
        $this->assign('seo_description', msubstr($articleInfo['article_abstract'], 0, 150));
//        dump($articleInfo);die;
        return $this->fetch('show');
    }

    private function get_recommend_news($condition = [])
    {
        // 获取推荐文章列表
        $condition['article_state'] = 3;
        $condition['article_commend_flag'] = 1; // 推荐文章标识
        list($recArticleList, $recArticleTotal) = D('cms_article')->getList($condition, 0, 5);
        $this->assign('recArticleList', $recArticleList);
    }

}
